package com.cloudpolice.door.controller;

import com.cloudpolice.door.feignInterface.DoorAuthorityInterFace;
import com.cloudpolice.door.feignInterface.EquipmentInterFace;
import com.cloudpolice.door.model.EquipmentSiteModel;
import com.cloudpolice.door.model.SnSiteModel;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.model.ResultVO;
import lh.model.ResultVOPage;
import lh.units.ResultStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/doorAuthorityFeignController")
@Api(value = "门禁机权限下载聚合层", description = "门禁机权限下载提供数据")
public class DoorAuthorityController {
    @Autowired
    DoorAuthorityInterFace doorAuthorityInterFace;
    @Autowired
    EquipmentInterFace equipmentInterFace;
    @Autowired
    Gson gson;
    private final String hystricMessage = "亲，服务器正忙，请稍后再戳。";

    /**
     * 测试，方法ID：SE202003051228101182BE3A25D56DF
     *
     * @param page 当前页数
     * @param limit 每页条数
     * @return 测试
     */
    @ApiOperation(value = "测试,方法ID：SE202003051228101182BE3A25D56DF", notes = "测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/getDoorPictureAuthorityTest")
    public ResultVOPage getDoorPictureAuthorityTest(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "limit", defaultValue = "10") int limit) {
        ResultVOPage resultValue = doorAuthorityInterFace.getDoorPictureAuthorityTest(
                page,
                limit);

        return resultValue;
    }

    /**
     * 测试，方法ID：SE202003051218568265A59B0386EE6
     *
     * @param page 当前页数
     * @param limit 每页条数
     * @return 测试
     */
    @ApiOperation(value = "测试,方法ID：SE202003051218568265A59B0386EE6", notes = "测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/getDoorCardAuthorityTest")
    public ResultVOPage getDoorCardAuthorityTest(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "limit", defaultValue = "10") int limit) {
        ResultVOPage resultValue = doorAuthorityInterFace.getDoorCardAuthorityTest(
                page,
                limit);

        return resultValue;
    }

    /**
     * 得到围墙机门禁机RFID卡权限
     *
     * @param sn    设备SN号
     * @param page  当前页数
     * @param limit 每页条数
     * @return
     */
    @ApiOperation(value = "得到门禁机RFID卡权限", notes = "LIST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "useId", value = "设备SN号(不可重复主键)", dataType = "String", required = true, defaultValue = "sn123456")
            , @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String")
            , @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/getDoorCardAuthority")
    public ResultVOPage getDoorCardAuthority(
            @RequestParam(value = "useId") String sn
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit
    ) {
        return getDoorAuthority(sn, (byte) 1, page, limit);
    }

    /**
     * 得到围墙机门禁机刷脸权限
     *
     * @param sn    设备SN号
     * @param page  当前页数
     * @param limit 每页条数
     * @return
     */
    @ApiOperation(value = "得到门禁机人脸权限", notes = "LIST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "useId", value = "设备SN号(不可重复主键)", dataType = "String", required = true, defaultValue = "sn123456")
            , @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String")
            , @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/getDoorPictureAuthority")
    public ResultVOPage getDoorPictureAuthority(
            @RequestParam(value = "useId") String sn
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit
    ) {
        return getDoorAuthority(sn, (byte) 0, page, limit);
    }

    private ResultVOPage getDoorAuthority(String sn, byte dateType, int page, int limit) {
        final SnSiteModel snSiteModel = setSite(sn);
        if (snSiteModel != null) {
            if (dateType == 0)
                return doorAuthorityInterFace.getDoorPictureAuthority(snSiteModel.getCommunityId()
                        , snSiteModel.getPeriodId()
                        , snSiteModel.getBuildId()
                        , snSiteModel.getUnitId()
                        , page
                        , limit);
            else if (dateType == 1)
                return doorAuthorityInterFace.getDoorCardAuthority(snSiteModel.getCommunityId()
                        , snSiteModel.getPeriodId()
                        , snSiteModel.getBuildId()
                        , snSiteModel.getUnitId()
                        , page
                        , limit);
            else
                return ResultStruct.error(hystricMessage, ResultVOPage.class);
        } else
            return ResultStruct.error(hystricMessage, ResultVOPage.class);
    }

    private SnSiteModel setSite(String sn) {
        final List<EquipmentSiteModel> snSite = getSnSite(sn);
        if (snSite == null) {
            return null;
        }
//        0:场所，1：区域，2：楼栋，3:单元，4：房间，5:通道
        SnSiteModel snSiteModel = new SnSiteModel();
        for (EquipmentSiteModel row :
                snSite) {
            switch (row.getRoomType()) {
                case 0:
                    snSiteModel.setCommunityId(row.getSiteId());
                    break;
                case 1:
                    snSiteModel.setPeriodId(row.getSiteId());
                    break;
                case 2:
                    snSiteModel.setBuildId(row.getSiteId());
                    break;
                case 3:
                    snSiteModel.setUnitId(row.getSiteId());
                    break;
                case 4:
                    break;
                case 5:
                    if (row.getSiteId() != null && !row.getSiteId().isEmpty()) {
                        snSiteModel.setBuildId(null);
                        snSiteModel.setUnitId(null);
                    }
                    break;
            }
        }
        return snSiteModel;
    }

    private List<EquipmentSiteModel> getSnSite(String sn) {
        List<EquipmentSiteModel> equipmentSiteModels = new ArrayList<>();
        final ResultVO resultVOEquipSite = equipmentInterFace.selectEquipmentSiteBySn(sn);
        if (resultVOEquipSite.getCode() == 0) {
            final List list = gson.fromJson(resultVOEquipSite.getData().toString(), List.class);
            for (Object row :
                    list) {
                equipmentSiteModels.add(gson.fromJson(row.toString(), EquipmentSiteModel.class));
            }
            return equipmentSiteModels;
        } else
            return null;
    }
}
